/*
Eteria IRC Client, an RFC 1459 compliant client program written in Java.
Copyright (C) 2000-2001  Javier Kohen <jkohen at tough.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.util.*;

public class Commands extends ListResourceBundle
{
    // Always use lower case for command tags! (See getCommand method below.)
	
    private static final Object [][] contents =
	{
    	// Standard IRC commands.
		
     	{ "admin", new Command("ADMIN", 0, 1) },
    	{ "away", new Command("AWAY", 0, 1) },
    	{ "connect", new Command("CONNECT", 1, 3) },
    	{ "info", new Command("INFO", 0, 1) },
    	{ "invite", new Command("INVITE", 2) },
		{ "ison", new Command("ISON", 1) },
		{ "join", new Command("JOIN", 1, 2) },
    	{ "kick", new Command("KICK", 2, 3) },
    	{ "kill", new Command("KILL", 2) },
    	{ "links", new Command("LINKS", 0, 2) },
    	{ "list", new Command("LIST", 0, 2) },
    	{ "lusers", new Command("LUSERS", 0, 2) },
    	{ "mode", new Command("MODE", 1, 4) },
    	{ "motd", new Command("MOTD", 0, 1) },
    	{ "names", new Command("NAMES", 0, 2) },
    	{ "notice", new Command("NOTICE", 2) },
    	{ "nick", new Command("NICK", 1) },
    	{ "oper", new Command("OPER", 2) },
		{ "part", new Command("PART", 0, 2) },
		{ "ping", new Command("PING", 1) },
    	{ "privmsg", new Command("PRIVMSG", 2) },
    	{ "quit", new Command("QUIT", 0, 1) },
		{ "rehash", new Command("REHASH", 0, 1) },
		{ "restart", new Command("RESTART", 0) },
    	{ "squit", new Command("SQUIT", 2) },
    	{ "stats", new Command("STATS", 0, 2) },
    	{ "time", new Command("TIME", 0, 1) },
    	{ "topic", new Command("TOPIC", 1, 2) },
    	{ "trace", new Command("TRACE", 0, 1) },
		{ "userhost", new Command("USERHOST", 1) },
    	{ "version", new Command("VERSION", 0, 1) },
    	{ "who", new Command("WHO", 0, 2) },
    	{ "whois", new Command("WHOIS", 1, 2) },
    	{ "whowas", new Command("WHOWAS", 1, 3) },

		// Internally handled commands.
		
		{ "clear", new Command("CLEAR", 0, 0) },
    	{ "ctcp", new Command("CTCP", 2) },
		{ "debug", new Command("DEBUG", 1) },
    	{ "eirc", new Command("EIRC", 0) },
		{ "ghost", new Command("GHOST", 2) },
		{ "help", new Command("HELP", 0, 1) },
    	{ "ignore", new Command("IGNORE", 0, 1)  },
		{ "j", "join" },
    	{ "kban", new Command("KBAN", 1, 3) },
    	{ "me", new Command("ME", 1) },
    	{ "msg", "privmsg" },
		{ "pingtime", new Command("PINGTIME", 1) },
    	{ "q", "query" },
    	{ "query", new Command("QUERY", 1, 2) },
		{ "quote", new Command("QUOTE", 1) },
    	{ "server", new Command("SERVER", 1, 3) },
    	{ "unignore", new Command("UNIGNORE", 1) },
		{ "w", "whois" },
    };

    public Object [][] getContents()
	{
		return contents;
    }

    /**
     * Parses command-line style input.
     *
     * @param text the string to parse.
     * @return a <code>String</code> array filled with the command name, followed by its parameters.
     * @throws MissingResourceException if the command can't be found by Commands.getCommand().
     * @throws IllegalArgumentException if there are missing parameters.
     */

    public static String [] parseCommand(ResourceBundle cmds, String text) throws MissingResourceException, IllegalArgumentException
	{
		StringTokenizer st = new StringTokenizer(text);

		/* Find command description. */

		// The next line may throw a MissingResourceException.
		Command c = Commands.getCommand(cmds, st.nextToken());
		String action = c.getTag();
		int required = c.getRequiredParameters();
		int accepted = c.getAcceptedParameters();

		Vector result = new Vector();

		result.addElement(action);

		/* Parse parameters. */

		final int tokens = st.countTokens();
		if (tokens < required)
		    throw new IllegalArgumentException("insuficient parameters");

		// Add parameters, omit last one because it needs special parsing.
		for (int i = 0; i < Math.min(accepted, tokens) - 1; i++)
	    	result.addElement(st.nextToken());

		if (tokens > 0)
		{
		    // Add the last parameter, which is allowed to contain spaces.
		    String tok = st.nextToken("");
		    result.addElement(tok.trim());
		}

		String [] ret = new String [result.size()];
		result.copyInto(ret);

		return ret;
    }

    /**
     * This method resolves Command aliases.
     */

    public static Command getCommand(ResourceBundle cmds, String key) throws MissingResourceException	 
	{			
		Object o = key.toLowerCase();

		while ((o = cmds.getObject((String) o)) instanceof String);
		return (Command) o;
    }
}
